import { IBagItemData } from "../Interface/IMessage";
import GameUtil from "../Util/GameUtil";
import StorageUtil from "../Util/StorageUtil";
import { EGameType, ELotteryType } from "./Enum";
import { TalkModule } from "./TalkModule";

/** 刮刮乐 */
export interface ILotteryConfig {
    /** 中奖率 */
    p: number;
    /** 奖励档次(数额及概率) */
    datas: {
        type: ELotteryType;
        /** 抽奖消耗 */
        needMoney: number;
        /** 视频抽奖 */
        isVideo: boolean;
        grades: ILotteryGrade[];
    }[];
}

export interface ILotteryGrade {
    id: number;
    max: number;
    p: number;
    ratio: number;
    values: number[];
}

/** 互怼配置 */
export interface IFightConfig {
    id: number;
    shoper: string;
    player: string;
    bubble: string;
    max: number;
}

/** 收集配置 */
export interface ICollectConfig {
    id: number;
    type: TalkModule;
    chance: number;
    isCollect: boolean;
    needs: { id: number, num: number }[];
}

export class GlobalData {
    public static design_width: number = 750;
    public static design_height: number = 1700;

    public static stage_width: number;
    public static stage_height: number;

    public static fixType: "宽适配" | "高适配" = '宽适配';
    public static recordType: '开始录' | '录屏中' | '待分享' = '开始录';

    public static maskOpacity: number = 180;

    public static isFast: boolean = false;

    public static gameView: EGameType = EGameType.Home;

    //------------------------------------------设置页头像------------------------------------
    private static settingIcons: { id: number, name: string }[] = [
        { id: 1, name: "顾云泽" },
        { id: 2, name: "顾浩" },
        { id: 3, name: "林瑶" },
        { id: 4, name: "富商" },
        { id: 5, name: "顾母" },
    ];
    public static getSettingIcon(id: number) {
        const config = this.settingIcons.find((o) => o.id === id);
        return config;
    }
    public static getSetingIcons() {
        return this.settingIcons;
    }
    //---------------------------------------------END--------------------------------------


    //---------------------------------------------收集-------------------------------------
    private static config: { id: number, ratio: number }[] = [
        { id: 0, ratio: 100, },
        { id: 1, ratio: 150, },
        { id: 2, ratio: 230, },
        { id: 3, ratio: 360, },
        { id: 4, ratio: 410, },
    ];
    public static getCollectConfigById(id: number) {
        return this.config.find((o) => o.id === id);
    }
    //---------------------------------------------END--------------------------------------


    //---------------------------------------------御康馆------------------------------------
    private static hospitalConfig: { id: number, name: string, need: number, reward: number }[] = [
        { id: 1, name: '药馆小工', need: 25, reward: 20 },
        { id: 2, name: '药馆伙计', need: 50, reward: 20 },
        { id: 3, name: '药馆学徒', need: 100, reward: 50 },
        { id: 4, name: '配药师', need: 200, reward: 50 },
        { id: 5, name: '普通医师', need: 400, reward: 100 },
        { id: 6, name: '中医大师', need: 0, reward: 100 },
    ]
    public static getHospitalConfig(id: number) {
        const config = this.hospitalConfig.find((o) => o.id === id);
        return config;
    }
    //---------------------------------------------END--------------------------------------

    //---------------------------------------------刮刮乐------------------------------------
    private static lotteryConfig: ILotteryConfig = {
        p: 43,
        datas: [
            {
                type: ELotteryType.Animal,
                needMoney: 1000,
                isVideo: false,
                grades: [
                    { id: 1, p: 47, max: 4, ratio: 1, values: [233, 648, 200, 666] },
                    { id: 2, p: 50, max: 1, ratio: 1, values: [2399, 3888, 1234, 1490, 2700, 3660, 1250, 2555, 7000, 8120] },
                    { id: 3, p: 2, max: 1, ratio: 1, values: [88948, 10086, 100001] },
                    { id: 4, p: 1, max: 1, ratio: 1, values: [200000] },
                ],
            },
            {
                type: ELotteryType.Mountain,
                needMoney: 20000,
                isVideo: false,
                grades: [
                    { id: 1, p: 4.4, max: 4, ratio: 1, values: [4399, 8888, 1234, 1490, 2700, 3660, 1250, 2555, 7000, 8120] },
                    { id: 2, p: 95, max: 1, ratio: 10000, values: [3, 2.1, 1.6, 5, 3.3, 3.5, 3.4] },
                    { id: 3, p: 0.5, max: 1, ratio: 10000, values: [25, 72, 16] },
                    { id: 4, p: 0.1, max: 1, ratio: 10000, values: [100] },
                ],
            },
            {
                type: ELotteryType.Rich,
                needMoney: 3000000,
                isVideo: false,
                grades: [
                    { id: 1, p: 97, max: 1, ratio: 10000, values: [450, 800, 600, 366, 66, 211, 111, 185] },
                    { id: 2, p: 1.5, max: 1, ratio: 10000, values: [3333, 1666, 4600, 1555, 7111, 6000] },
                    { id: 3, p: 1, max: 1, ratio: 100000000, values: [1, 2, 3, 5] },
                    { id: 4, p: 0.4, max: 1, ratio: 100000000, values: [10, 15] },
                    { id: 5, p: 0.1, max: 1, ratio: 100000000, values: [20] },
                ],
            },
            {
                type: ELotteryType.Fruit,
                needMoney: 10000000,
                isVideo: false,
                grades: [
                    { id: 1, p: 42, max: 1, ratio: 10000, values: [450, 800, 600, 366, 466, 211, 111, 985] },
                    { id: 2, p: 55, max: 1, ratio: 10000, values: [1333, 1666, 1600, 1555, 111, 800] },
                    { id: 3, p: 1.8, max: 1, ratio: 100000000, values: [1, 2, 3, 5] },
                    { id: 4, p: 1, max: 1, ratio: 100000000, values: [10, 15, 22, 28, 38] },
                    { id: 5, p: 0.2, max: 1, ratio: 100000000, values: [40] },
                ],
            },
            {
                type: ELotteryType.Gress,
                needMoney: 80000000,
                isVideo: false,
                grades: [
                    { id: 1, p: 6, max: 1, ratio: 10000, values: [450, 800, 600, 366, 466, 211, 111, 985] },
                    { id: 2, p: 90, max: 1, ratio: 10000, values: [3333, 1666, 4600, 8555, 8111, 8000] },
                    { id: 3, p: 3, max: 1, ratio: 100000000, values: [1, 2, 3, 5] },
                    { id: 4, p: 0.8, max: 1, ratio: 100000000, values: [10, 15, 22, 28, 38, 66, 77, 50, 45] },
                    { id: 5, p: 0.2, max: 1, ratio: 100000000, values: [80] },
                ],
            },
            {
                type: ELotteryType.Gold,
                needMoney: 200000000,
                isVideo: false,
                grades: [
                    { id: 1, p: 5, max: 1, ratio: 10000, values: [450, 800, 600, 366, 466, 211, 111, 985] },
                    { id: 2, p: 15, max: 1, ratio: 10000, values: [3333, 1666, 4600, 1555, 7111, 6000] },
                    { id: 3, p: 70, max: 1, ratio: 100000000, values: [1, 2, 3, 5] },
                    { id: 4, p: 8, max: 1, ratio: 100000000, values: [10, 15, 22, 28, 38, 66, 77, 50, 45] },
                    { id: 5, p: 1.5, max: 1, ratio: 100000000, values: [111, 133, 166, 188] },
                    { id: 6, p: 0.5, max: 1, ratio: 100000000, values: [200] },
                ],
            },
            {
                type: ELotteryType.Penalty,
                needMoney: 0,
                isVideo: true,
                grades: [
                    { id: 1, p: 90, max: 4, ratio: 1, values: [233, 648, 200, 666] },
                    { id: 2, p: 50, max: 4, ratio: 1, values: [4399, 8888] },
                    { id: 3, p: 8, max: 1, ratio: 100, values: [450, 800, 600, 366, 466, 211, 111, 985] },
                    { id: 4, p: 0.05, max: 1, ratio: 100, values: [3333, 1666, 4600, 1555, 7111, 6000] },
                    { id: 5, p: 0.0009, max: 1, ratio: 100000000, values: [1, 2, 3, 5] },
                    { id: 6, p: 0.00001, max: 1, ratio: 100000000, values: [200] },
                ],
            }
        ],

    }
    public static lotteryType: ELotteryType = ELotteryType.Animal;
    public static deepCopyLotteryData() {
        const config = GameUtil.deepCopy(this.lotteryConfig) as ILotteryConfig;
        const data = config.datas.find((o) => o.type === this.lotteryType);
        return { p: config.p, grades: data.grades };
    }
    public static getLotteryConfig(type: ELotteryType) {
        const config = this.lotteryConfig.datas.find((o) => o.type === type);
        return config;
    }
    //---------------------------------------------END------------------------------------


    //---------------------------------------------互怼------------------------------------
    private static fightTalk: IFightConfig[] = [
        {
            id: 1,
            shoper: '我这店铺在矿村村中心，是绝对的旺铺！少了1000万不行！',
            player: '危房三级，纸糊的窗户，漏水的棚，我多出一个子都是对他们的不尊重。',
            bubble: '中心！C位不懂吗？/穷鬼别来沾边/没钱就别买',
            max: 6,
        },
        {
            id: 2,
            shoper: '矿村小卖店仅我一家！物以稀为贵你懂不懂啊！',
            player: '是，你同行早搬去市区十年了，也就剩你一家在矿村吃苦。',
            bubble: '含金量100%/矿村独苗/垄断生意',
            max: 10,
        },
        {
            id: 3,
            shoper: '我手里掌握着村里的一线情报，原价买了小卖部，这个给你当赠品',
            player: '村口情报处消息全还确切，怎么也比你收费的合适',
            bubble: '豪华赠品/消息就是金钱！/便宜你了',
            max: 15,
        },
        {
            id: 4,
            shoper: '矿村就我口碑好，兑给你以后都是客源！',
            player: '矿村全村人，算上村里的老鼠，地里的麻雀，村口的驴，也就有38口。你还把谁算成客源了。',
            bubble: '好口碑等于好客源/矿村人只认我',
            max: 30,
        },
    ];
    // public static successTalk: string = `没想到你小子，居然还有两下子。那就按照你说的100万`;
    public static getFightConfig(id: number) {
        const config = this.fightTalk.find((o) => o.id === id);
        return config;
    }

    private static fightConfig: { angle: number, start: cc.Vec2, end: cc.Vec2 }[] = [
        { angle: 20, start: cc.v2(170, -425), end: cc.v2(-111, 350) },
        { angle: 25, start: cc.v2(213, -400), end: cc.v2(-144, 370) },
        { angle: 22, start: cc.v2(205, -405), end: cc.v2(-115, 385) },
    ]
    public static getFightRandom() {
        const index = Math.floor(Math.random() * this.fightConfig.length);
        return this.fightConfig[index];
    }
    //---------------------------------------------END------------------------------------


    //---------------------------------------------背包------------------------------------
    public static getBagDataById(id: number) {
        const bagData = StorageUtil.instance.getBagData();
        const data = bagData.find((o) => o.id === id);
        return data;
    }
    public static addBagData(id: number, num: number = 1, callback: Function = null) {
        const bagData = StorageUtil.instance.getBagData();
        const find = bagData.find((o) => o.id === id);
        if (!find) {
            const newData: IBagItemData = { id: id, num: num };
            bagData.push(newData);
        } else {
            find.num += num;
        }

        callback && callback();
        callback = null;
    }
    public static removeBagData(id: number, num: number = 1, callback?: Function) {
        const bagData = StorageUtil.instance.getBagData();
        for (let i = 0; i < bagData.length; i++) {
            const data = bagData[i];
            if (data.id === id) {
                data.num -= num;
                if (data.num <= 0) {
                    bagData.splice(i, 1);
                    i--;
                }
                break;
            }
        }

        callback && callback();
        callback = null;
    }
    public static checkBagHad(id: number, value: number = 1) {
        const bagData = StorageUtil.instance.getBagData();
        const data = bagData.find((o) => o.id === id);
        if (data) {
            return data.num >= value;
        }
        return false;
    }
    //---------------------------------------------END------------------------------------


    //---------------------------------------------商人------------------------------------
    public static collects: ICollectConfig[] = [
        {
            id: 1,
            type: TalkModule.COLLECT_TIEBAN,
            chance: 50,
            isCollect: false,
            needs: [{ id: 10002, num: 1 }],
        },
        {
            id: 2,
            type: TalkModule.COLLECT_ANIMAL,
            chance: 25,
            isCollect: true,
            needs: [
                { id: 10016, num: 1 }, { id: 10017, num: 1 }, { id: 10018, num: 1 }, { id: 10019, num: 1 }, { id: 10020, num: 1 }, { id: 10021, num: 1 },
                { id: 10022, num: 1 }, { id: 10023, num: 1 }, { id: 10024, num: 1 }, { id: 10025, num: 1 }, { id: 10026, num: 1 }, { id: 10027, num: 1 },
            ],
        },
        {
            id: 3,
            type: TalkModule.COLLECT_FIVE,
            chance: 25,
            isCollect: true,
            needs: [
                { id: 10011, num: 1 }, { id: 10012, num: 1 }, { id: 10013, num: 1 }, { id: 10014, num: 1 }, { id: 10015, num: 1 }
            ],
        },
    ];

    public static checkCollectComplete() {
        const data = this.getCollectData();
        if (!data) return false;
        if (data.needs) {
            for (let i = 0; i < data.needs.length; i++) {
                const need = data.needs[i];
                const bagData = GlobalData.getBagDataById(need.id);
                if (!bagData) return false;
                if (bagData?.num < need?.num) {
                    return false;
                }
            }
        }
        return true;
    }

    // public refresh() {
    //     const realPrice = this.getRealPrice();
    //     this.labName.string = `${this.config.name}`;
    //     this.labHaveNum.string = `${this.bagData?.num || 0}`;
    //     this.labNeedNum.string = `/${this.data.num}`;
    // }

    public static getCollectData() {
        const taskData = StorageUtil.instance.getTalkData();
        const collectData = this.collects.find((o) => o.type === taskData.id);
        return collectData;
    }

    public static deleteCollect() {
        const taskData = StorageUtil.instance.getTalkData();
        const collectData = this.collects.find((o) => o.type === taskData.id);
        collectData.needs.forEach((o) => {
            this.removeBagData(o.id, o.num);
        });
    }

    public static checkCollect() {
        const collectData = this.getCollectData();
        if (collectData) {
            for (let i = 0; i < collectData.needs.length; i++) {
                const data = collectData.needs[i];
                const find = GlobalData.getBagDataById(data.id);
                if (!find || find?.num < data.num) {
                    return false;
                }
            }
            return true;
        }
        return '当前没收集任务';
    }
    //---------------------------------------------END------------------------------------
}